package net.hn.hnms.biz.gasext.dws.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.util.DateUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.api.safety.location.model.LeadVo;
import net.hn.hnms.biz.gasext.dws.domain.dto.*;
import net.hn.hnms.biz.gasext.dws.domain.vo.*;
import net.hn.hnms.biz.gasext.dws.mapper.*;
import net.hn.hnms.biz.gasext.dws.service.AqjkService;
import net.hn.hnms.biz.gasext.dws.service.DwdRydwService;
import net.hn.hnms.biz.gasext.dws.service.DwsService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.redis.domain.SysOrganization;
import net.hn.hnms.biz.gasext.dws.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName DwdRydwServiceImpl
 * @Description
 * @Author halo
 * @Date 2024/8/28 14:02
 **/
@Service
@DS(value = "dwd")
public class DwdRydwServiceImpl implements DwdRydwService {

    @Resource
    private DwdRydwAlarmDetailMapper dwdRydwAlarmDetailMapper;
    @Resource
    private DwdRydwRyssHMapper dwdRydwRyssHMapper;
    @Resource
    private AqjkService aqjkService;


    @Resource
    private DwdRydwRyssDMapper dwdRydwRyssDMapper;
    @Resource
    private JyglShiftManagementMapper jyglShiftManagementMapper;

    @Resource
    private JyglInsertShiftManage1Mapper jyglInsertShiftManage1Mapper;

    @Resource
    private JyglDwmcEntityMapper jyglDwmcEntityMapper;
    @Resource
    private DimCoalMineBasicInfoMapper dimCoalMineBasicInfoMapper;

    @Resource
    private DwsService dwsService;


    @Resource
    private DwdRyDdwRyqjRtDFMapper dwdRyDdwRyqjRtDFMapper;
    @Resource
    private DwdRyDdwRycyRtDFMapper dwdRyDdwRycyRtDFMapper;
    @Resource
    private DwdRyDdwRycsRtDFMapper dwdRyDdwRycsRtDFMapper;

    @Resource
    private DwdRyDdwRyxzRtDFMapper dwdRyDdwRyxzRtDFMapper;

    @Resource
    private DwdRydwRyjzDMapper dwdRydwRyjzDMapper;
    @Resource
    private DwdRydwRyqyDMapper dwdRydwRyqyDMapper;
    @Resource
    private DwdRydwRyxxDMapper dwdRydwRyxxDMapper;

    @Resource
    private DwdRydwRyxzDistinctRtDMapper dwdRydwRyxzDistinctRtDMapper;
    @Resource
    private DwdRydwRycsDistinctRtDMapper dwdRydwRycsDistinctRtDMapper;
    @Resource
    private DwdRydwRyqjDistinctRtDMapper dwdRydwRyqjDistinctRtDMapper;
    @Resource
    private DwdRydwRycyDistinctRtDMapper dwdRydwRycyDistinctRtDMapper;
    /**
     * 查询报警明细
     *
     * @param alarmDetailDto
     * @return
     */
    @Override
    public ResponsePage<AlarmDetailVO> selectAlarmDetailList(
            PageQuery pageQuery, String companyCode, AlarmDetailDto alarmDetailDto) {

        pageQuery = dealWithPageQuery(pageQuery);

        List<String> mineCodeList = getMineCodeListByCompanyCode(alarmDetailDto);

        if (StringUtils.isNotEmpty(alarmDetailDto.getMineCode())) {
            mineCodeList = null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isEmpty(alarmDetailDto.getStartTime())) {
            //开始时间为空 给与默认值现在的时间往前推1一天
            Date startDateTime = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(new Date(), -1);
            alarmDetailDto.setStartTime(sdf.format(startDateTime));
        }
        if (StringUtils.isEmpty(alarmDetailDto.getEndTime())) {
            alarmDetailDto.setEndTime(sdf.format(new Date()));
        }

        List<AlarmDetailVO> detailVOList = new ArrayList<>();
        Integer total = 0;
        //超时报警 personOvertime
        if ("areaOvercrowding".equals(alarmDetailDto.getAlarmType())) {
            total = dwdRydwAlarmDetailMapper.selectAreaOvercrowdingTotal(mineCodeList, alarmDetailDto);
            if (total > 0) {
                detailVOList = dwdRydwAlarmDetailMapper.selectAreaOvercrowdingList(
                        pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, alarmDetailDto);
            }
            //超员报警 areaOvercrowding
        } else if ("personOvertime".equals(alarmDetailDto.getAlarmType())) {
            total = dwdRydwAlarmDetailMapper.selectPersonOvertimeTotal(mineCodeList, alarmDetailDto);
            if (total > 0) {
                detailVOList = dwdRydwAlarmDetailMapper.selectPersonOvertimeList(
                        pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, alarmDetailDto);
            }
            //人员求救 personHelp
        } else if ("personHelp".equals(alarmDetailDto.getAlarmType())) {
            total = dwdRydwAlarmDetailMapper.selectPersonHelpTotal(mineCodeList, alarmDetailDto);
            if (total > 0) {
                detailVOList = dwdRydwAlarmDetailMapper.selectPersonHelpList(
                        pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, alarmDetailDto);
            }
            //进入限制区域报警 areaDisallowed
        } else if ("areaDisallowed".equals(alarmDetailDto.getAlarmType())) {
            total = dwdRydwAlarmDetailMapper.selectAreaDisallowedTotal(mineCodeList, alarmDetailDto);
            if (total > 0) {
                detailVOList = dwdRydwAlarmDetailMapper.selectAreaDisallowedList(
                        pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, alarmDetailDto);
            }
        } else {
            // 查询全部四个表，排序
            int total1 = dwdRydwAlarmDetailMapper.selectAreaOvercrowdingTotal(mineCodeList, alarmDetailDto);
            int total2 = dwdRydwAlarmDetailMapper.selectPersonOvertimeTotal(mineCodeList, alarmDetailDto);
            int total3 = dwdRydwAlarmDetailMapper.selectPersonHelpTotal(mineCodeList, alarmDetailDto);
            int total4 = dwdRydwAlarmDetailMapper.selectAreaDisallowedTotal(mineCodeList, alarmDetailDto);
            total = total1 + total2 + total3 + total4;

            if (total > 0) {
                detailVOList = dwdRydwAlarmDetailMapper.selectAllTypeList(
                        pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, alarmDetailDto);
            }
        }
        detailVOList = (List<AlarmDetailVO>) setMineAndCompanyInfo(detailVOList);

        return new ResponsePage<>(detailVOList, total);
    }

    @Override
    public ResponsePage<DwdRydwRealTimeDataVo> selectRealTimeDataList(PageQuery pageQuery, DwdRydwRealTimeDataDto dto) {

        pageQuery = dealWithPageQuery(pageQuery);

        List<String> mineCodeList = getMineCodeListByCompanyCode(dto);

        if (StringUtils.isNotEmpty(dto.getMineCode())) {
            mineCodeList = null;
        }
        if (StringUtils.isNotEmpty(dto.getMineCode())) {
            List<DwdRydwRyssDVo> list = new ArrayList<>();
            //煤矿编码不为空
            if (StringUtils.isEmpty(dto.getDeptName()) && StringUtils.isEmpty(dto.getTypeOfWork()) && StringUtils.isEmpty(dto.getName()) && StringUtils.isEmpty(dto.getIsLeader())) {
                //如果除了煤矿编码 所有入参都是空的则 直接查询最新的人员定位实时数据
                list = dwdRydwRyssHMapper.selectRyssListByMineCode(dto.getMineCode(),pageQuery,dto.getIsSftzry());
            } else {
                //按照部门 工种 姓名 是否矿领导 查询所有的人员卡编码列表
                List<String> jzbmList = dwdRydwRyxxDMapper.selectRyxxList(dto.getMineCode(), dto.getDeptName(), dto.getName(), dto.getTypeOfWork(),dto.getIsLeader(),dto.getIsSftzry());
                jzbmList = jzbmList.stream()
                        .filter(Objects::nonNull)  // 移除 null 值
                        .distinct()                // 去重
                        .collect(Collectors.toList()); // 收集成 List
                //根据煤矿编码 和人员卡编码列表 查询当前煤矿的最新数据列表
                if (jzbmList.isEmpty()) {
                return new ResponsePage<>(new ArrayList<>(), 0);
                }
                list = dwdRydwRyssHMapper.selectRyssListByMineCodeAndJzbmList(dto.getMineCode(), jzbmList, pageQuery);
            }
            //获取当前煤矿所有的人员基本信息列表
            List<DwdRydwRyxxDPo> dwdRydwRyxxDPos = dwdRydwRyxxDMapper.selectRyxxListByMineCode(dto.getMineCode());
            //根据人员卡编码转成map 过滤为空的人员卡编码
            Map<String, DwdRydwRyxxDPo> dwdRydwRyxxDPoMap = dwdRydwRyxxDPos.stream()
                    .filter(po -> po.getRykbm() != null) // 过滤掉 rykbm 为 null 的对象
                    .collect(Collectors.toMap(DwdRydwRyxxDPo::getRykbm, Function.identity(),(existing, replacement) -> existing));

            //获取当前煤矿的所有基站信息列表
            List<DwdRydwRyjzDPo> dwdRydwRyjzDPos = dwdRydwRyjzDMapper.selectRyjzListByMineCode(dto.getMineCode());
            //根据基站编码转成map
            Map<String, DwdRydwRyjzDPo> dwdRydwRyjzDPoMap = dwdRydwRyjzDPos.stream()
                    .filter(po -> po.getJzbm() != null) // 过滤掉 jzbm 为 null 的对象
                    .collect(Collectors.toMap(DwdRydwRyjzDPo::getJzbm, Function.identity(),(existing, replacement) -> existing));
            //构建返回对象
            List<DwdRydwRealTimeDataVo> collect = list.stream().map(ttt -> {
                DwdRydwRealTimeDataVo vo = new DwdRydwRealTimeDataVo();
                vo.setMineCode(ttt.getMkbm());
                vo.setTotal(ttt.getTotal());
                vo.setMineName(ttt.getKjmc()!=null?ttt.getKjmc():"---");
                DwdRydwRyxxDPo dwdRydwRyxxDPo = dwdRydwRyxxDPoMap.get(ttt.getRykbm());
                if (dwdRydwRyxxDPo != null) {
                    vo.setName(dwdRydwRyxxDPo.getXm() == null ? "---" : dwdRydwRyxxDPo.getXm());
                    //部门
                    vo.setDeptName(dwdRydwRyxxDPo.getDzbzbm() == null ? "---" : dwdRydwRyxxDPo.getDzbzbm());
                    //工种
                    vo.setTypeOfWork(dwdRydwRyxxDPo.getGz() == null ? "---" : dwdRydwRyxxDPo.getGz());
                    //是否矿领导
                    vo.setIsLeader(dwdRydwRyxxDPo.getSfkld() == null ? "---" : dwdRydwRyxxDPo.getSfkld());
                    //人员卡编码
                    vo.setPersonCardCode(ttt.getRykbm()!=null?ttt.getRykbm(): "---");
                    //数据上传时间
                    vo.setDateUpTime(ttt.getSjscsj() != null ? ttt.getSjscsj() : "---");
                } else {
                    vo.setName("---");
                    vo.setDeptName("---");
                    vo.setTypeOfWork("---");
                    vo.setIsLeader("---");
                }
                DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDPoMap.get(ttt.getJzbm());
                if (dwdRydwRyjzDPo != null) {
                    vo.setCurrentPosition(dwdRydwRyjzDPo.getJzmc() != null ? dwdRydwRyjzDPo.getJzmc() : "---");
                } else {
                    vo.setCurrentPosition("---");
                }
                //到达时间
                vo.setArrivalTime(ttt.getJrdqscjzsk() != null ? ttt.getJrdqscjzsk() : "---");
                //下井时间
                vo.setDescendWellTime(ttt.getRjsk() != null ? ttt.getRjsk() : "---");
                return vo;
            }).collect(Collectors.toList());
            return new ResponsePage<>(collect, collect.size() >= 1 ? collect.get(0).getTotal() : 0);
        }
        throw new HuaNengException(Constants.FAIL, "煤矿编码不能为空");
//        List<DwdRydwRealTimeDataVo> list = dwdRydwRyssHMapper.selectRealTimeDataList(
//                pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, dto);
//
//        list = (List<DwdRydwRealTimeDataVo>) setMineAndCompanyInfo(list);
//
//        list.stream().forEach(obj -> {
//            DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDMapper.selectOneByJzbm(obj.getCurrentPosition());
//            if(dwdRydwRyjzDPo != null){
//                obj.setCurrentPosition(dwdRydwRyjzDPo.getJzmc());
//            }
//        });
//        return new ResponsePage<>(list, list.size() >= 1 ? list.get(0).getTotal() : 0);
    }
    @Override
    public ResponsePage<DwdRydwHistoricalDataVo> selectHistoricalDataList(PageQuery pageQuery, DwdRydwHistoricalDataDto dto) {

        pageQuery = dealWithPageQuery(pageQuery);

        List<String> mineCodeList = getMineCodeListByCompanyCode(dto);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isEmpty(dto.getStartTime())) {
            //开始时间为空 给与默认值现在的时间往前推1一天
            Date startDateTime = net.hn.hnms.sys.common.core.utils.DateUtils.addHours(new Date(), -1);
            dto.setStartTime(sdf.format(startDateTime));
        }
        if (StringUtils.isEmpty(dto.getEndTime())) {
            dto.setEndTime(sdf.format(new Date()));
        }

        if (StringUtils.isNotEmpty(dto.getMineCode())) {
            mineCodeList = null;
        }
        if (StringUtils.isNotEmpty(dto.getMineCode())) {
            List<DwdRydwRyssDVo> list = new ArrayList<>();
            //煤矿编码不为空
            if (StringUtils.isEmpty(dto.getDeptName()) && StringUtils.isEmpty(dto.getTypeOfWork()) && StringUtils.isEmpty(dto.getName()) && StringUtils.isEmpty(dto.getIsLeader())) {
                //如果除了煤矿编码 所有入参都是空的则 直接查询最新的人员定位实时数据
                list = dwdRydwRyssDMapper.selectRyssListByMineCode(dto.getMineCode(),pageQuery, dto.getStartTime(), dto.getEndTime(),dto.getIsSftzry());
            } else {
                //按照部门 工种 姓名 是否矿领导 查询所有的人员卡编码列表
                List<String> jzbmList = dwdRydwRyxxDMapper.selectRyxxList(dto.getMineCode(), dto.getDeptName(), dto.getName(), dto.getTypeOfWork(), dto.getIsLeader(), dto.getIsSftzry());
                jzbmList = jzbmList.stream()
                        .filter(Objects::nonNull)  // 移除 null 值
                        .distinct()                // 去重
                        .collect(Collectors.toList()); // 收集成 List
                //根据煤矿编码 和人员卡编码列表 查询当前煤矿的最新数据列表
                if (jzbmList.isEmpty()) {
                    return new ResponsePage<>(new ArrayList<>(), 0);
                }
                list = dwdRydwRyssDMapper.selectRyssListByMineCodeAndJzbmList(dto.getMineCode(), jzbmList, pageQuery,dto.getStartTime(), dto.getEndTime());
            }
            //获取当前煤矿所有的人员基本信息列表
            List<DwdRydwRyxxDPo> dwdRydwRyxxDPos = dwdRydwRyxxDMapper.selectRyxxListByMineCode(dto.getMineCode());
            //根据人员卡编码转成map 过滤为空的人员卡编码
            Map<String, DwdRydwRyxxDPo> dwdRydwRyxxDPoMap = dwdRydwRyxxDPos.stream()
                    .filter(po -> po.getRykbm() != null) // 过滤掉 rykbm 为 null 的对象
                    .collect(Collectors.toMap(DwdRydwRyxxDPo::getRykbm, Function.identity(),(existing, replacement) -> existing));

            //获取当前煤矿的所有基站信息列表
            List<DwdRydwRyjzDPo> dwdRydwRyjzDPos = dwdRydwRyjzDMapper.selectRyjzListByMineCode(dto.getMineCode());
            //根据基站编码转成map
            Map<String, DwdRydwRyjzDPo> dwdRydwRyjzDPoMap = dwdRydwRyjzDPos.stream()
                    .filter(po -> po.getJzbm() != null) // 过滤掉 jzbm 为 null 的对象
                    .collect(Collectors.toMap(DwdRydwRyjzDPo::getJzbm, Function.identity(),(existing, replacement) -> existing));
            //构建返回对象
            List<DwdRydwRealTimeDataVo> collect = list.stream().map(ttt -> {
                DwdRydwRealTimeDataVo vo = new DwdRydwRealTimeDataVo();
                vo.setMineCode(ttt.getMkbm());
                vo.setTotal(ttt.getTotal());
                vo.setMineName(ttt.getKjmc()!=null?ttt.getKjmc():"---");
                DwdRydwRyxxDPo dwdRydwRyxxDPo = dwdRydwRyxxDPoMap.get(ttt.getRykbm());
                if (dwdRydwRyxxDPo != null) {
                    vo.setName(dwdRydwRyxxDPo.getXm() == null ? "---" : dwdRydwRyxxDPo.getXm());
                    //部门
                    vo.setDeptName(dwdRydwRyxxDPo.getDzbzbm() == null ? "---" : dwdRydwRyxxDPo.getDzbzbm());
                    //工种
                    vo.setTypeOfWork(dwdRydwRyxxDPo.getGz() == null ? "---" : dwdRydwRyxxDPo.getGz());
                    //是否矿领导
                    vo.setIsLeader(dwdRydwRyxxDPo.getSfkld() == null ? "---" : dwdRydwRyxxDPo.getSfkld());
                    //人员卡编码
                    vo.setPersonCardCode(ttt.getRykbm() != null ? ttt.getRykbm() : "---");
                    //数据上传时间
                    vo.setDateUpTime(ttt.getSjscsj() != null ? ttt.getSjscsj() : "---");
                } else {
                    vo.setName("---");
                    vo.setDeptName("---");
                    vo.setTypeOfWork("---");
                    vo.setIsLeader("---");
                }
                DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDPoMap.get(ttt.getJzbm());
                if (dwdRydwRyjzDPo != null) {
                    vo.setCurrentPosition(dwdRydwRyjzDPo.getJzmc() != null ? dwdRydwRyjzDPo.getJzmc() : "---");
                } else {
                    vo.setCurrentPosition("---");
                }
                //到达时间
                vo.setArrivalTime(ttt.getJrdqscjzsk() != null ? ttt.getJrdqscjzsk() : "---");
                //下井时间
                vo.setDescendWellTime(ttt.getRjsk() != null ? ttt.getRjsk() : "---");
                return vo;
            }).collect(Collectors.toList());
            return new ResponsePage<>(collect, collect.size() >= 1 ? collect.get(0).getTotal() : 0);
        }
        throw new HuaNengException(Constants.FAIL, "煤矿编码不能为空");
//        List<DwdRydwHistoricalDataVo> list = dwdRydwRyssDMapper.selectHistoricalDataList(
//                pageQuery.getPageSize(), pageQuery.getPageNum(), mineCodeList, dto);
//
//        list = (List<DwdRydwHistoricalDataVo>) setMineAndCompanyInfo(list);
//        return new ResponsePage<>(list, list.size() >= 1 ? list.get(0).getTotal() : 0);
    }

    /**
     * pc-人员定位-实时监测
     * @return
     */
    @Override
    public CompanyMonitoringVo monitoring(String companyCode, String mineCode) {
        CompanyMonitoringVo vo = new CompanyMonitoringVo();
        List<MinePersonDataDto> coutList = new ArrayList<>();
        List<MinePersonDataDto> leaderNumberList = new ArrayList<>();
        List<MinePersonDataDto> shiftLeadNumberList = new ArrayList<>();
        List<MinePersonDataDto> specialNumberList = new ArrayList<>();
        List<MinePersonDataDto> overStaffMineNumberList = new ArrayList<>();
        List<MinePersonDataDto> overTwoHundredMineNumberList = new ArrayList<>();
        List<MinePersonDataDto> severeDisasterOverStaffMineNumberList = new ArrayList<>();
        List<MinePersonDataDto> noShiftLeadNumberNumberList = new ArrayList<>();

        //获取帆软所有煤矿编码名称映射
        List<JyglDwmcEntityPo> jyglDwmcEntityPos = jyglDwmcEntityMapper.selectList(null);
        //根据煤矿编码转成map
        Map<String, JyglDwmcEntityPo> jyglDwmcEntityPoMap = jyglDwmcEntityPos.stream()
                .filter(p -> p.getOrgId() != null)
                .collect(Collectors.toMap(JyglDwmcEntityPo::getOrgId, p -> Optional.ofNullable(p).orElse(new JyglDwmcEntityPo())));
        //获取帆软所有煤矿时段信息
        List<JyglInsertShiftManage1Po> jyglInsertShiftManage1PoList = jyglInsertShiftManage1Mapper.selectList(null);
        //根据煤矿名称转成map
        Map<String, JyglInsertShiftManage1Po> jyglInsertShiftManage1PoMap = jyglInsertShiftManage1PoList.stream()
//                重复键值保留最新的值
                .collect(Collectors.toMap(JyglInsertShiftManage1Po::getDw, p -> Optional.ofNullable(p).orElse(new JyglInsertShiftManage1Po()), (existing, replacement) -> replacement));
        //获取帆软当天值班信息列表
        List<JyglShiftManagementPo> jyglShiftManagementPos = jyglShiftManagementMapper.getFineDataBeOnWatchListBymineName(null);
        //根据煤矿名称转成map
        Map<String, List<JyglShiftManagementPo>> jyglShiftManagementPoMap = jyglShiftManagementPos.stream()
                .filter(po -> po.getDw() != null) // 过滤掉键为 null 的元素
                .collect(Collectors.groupingBy(JyglShiftManagementPo::getDw));
        //获取所有煤矿井下人数
        List<DimCoalMineBasicInfoPo> dimCoalMineBasicInfoPos = aqjkService.selectCodeSubMineInfoList(companyCode, mineCode);
        dimCoalMineBasicInfoPos.forEach(dimCoalMineBasicInfoPo -> {
            //构建返回弹窗
            MinePersonDataDto minePersonDataDto = new MinePersonDataDto();
            String mkCode = dimCoalMineBasicInfoPo.getMkCode();
            //获取当前煤矿的井下人数
            Integer peopleNumber = dwdRydwRyssHMapper.selectPersonNumberList(mkCode);
            //井下人数
            vo.setCount(vo.getCount() + peopleNumber);
            //获取当前煤矿的井下领导数
            List<LeadVo> leadVos = dwdRydwRyssHMapper.selectLeadPitInfo(mkCode);
            Integer leadNumber = leadVos.size();
            //井下领导数
            vo.setLeaderNumber(vo.getLeaderNumber() + leadNumber);
            //获取当前煤矿特种作业人数
            Integer specialNumber = dwdRydwRyssHMapper.selectSpecialNumberList(mkCode);
            vo.setSpecialNumber(vo.getSpecialNumber() + specialNumber);
            //获取煤矿核定人数
            String approvedPersonCapacityQty = dimCoalMineBasicInfoPo.getApprovedPersonCapacityQty();
            //超员矿井数量 判断当前煤矿是否超员
            Boolean isOverStaffMine = false;
            if (StringUtils.isNotEmpty(approvedPersonCapacityQty) && Integer.parseInt(approvedPersonCapacityQty) < peopleNumber) {
                //超员矿井
                isOverStaffMine = true;
                vo.setOverStaffMineNumber(vo.getOverStaffMineNumber() + 1);
            }
            if (peopleNumber >= 200) {
                //超200人矿井
                vo.setOverTwoHundredMineNumber(vo.getOverTwoHundredMineNumber() + 1);
            }
            //判断当前煤矿是否是灾害严重矿井
            //获取当前煤矿灾害
            String mineDisasterTypes = dimCoalMineBasicInfoPo.getMineDisasterTypes();
            boolean severeDisasterOverStaffMine = this.isSevereDisasterOverStaffMine(mineDisasterTypes);
            if (severeDisasterOverStaffMine && isOverStaffMine) {
                //灾害严重矿井且超员煤矿
                vo.setSevereDisasterOverStaffMineNumber(vo.getSevereDisasterOverStaffMineNumber() + 1);
            }
            //根据煤矿编码获取当前煤矿名称
            JyglDwmcEntityPo jyglDwmcEntityPo = jyglDwmcEntityPoMap.get(mkCode);
            TreeSet<String> minShiftList = new TreeSet<>();
            String mineName = "";
            //获取带班领导数
            Integer minShiftNumber= 0;
            //可以查到煤矿
            if (jyglDwmcEntityPo != null) {
                 mineName = jyglDwmcEntityPo.getFourdw();
                //获取时段信息
                JyglInsertShiftManage1Po jyglInsertShiftManage1Po = jyglInsertShiftManage1PoMap.get(mineName);
                //获取当前煤矿带值班信息
                List<JyglShiftManagementPo> fineDataBeOnWatchListBymineName = jyglShiftManagementPoMap.get(mineName);
                if (fineDataBeOnWatchListBymineName != null) {
                    //判断当前班次
                    String classes = getShiftTimeList(new Date(), jyglInsertShiftManage1Po);
                    //获取带班领导信息
                    minShiftList = getMinShiftListByClasses(fineDataBeOnWatchListBymineName, classes);
                    //获取带班领导数
                     minShiftNumber = minShiftList.size();
                    //无带班领导 累加
                    if (minShiftNumber == 0) {
                        vo.setNoShiftLeadNumberNumber(vo.getNoShiftLeadNumberNumber() + 1);
                    }
                    if (minShiftNumber != 0) {
                        vo.setShiftLeadNumber(vo.getShiftLeadNumber() + minShiftList.size());
                    }
                }
            } else {
                //查不到煤矿  没有该煤矿信息 无领导名称
            }


            //煤矿名称
            minePersonDataDto.setMineName(dimCoalMineBasicInfoPo.getMkFullName());
            minePersonDataDto.setMineCode(mkCode);
            minePersonDataDto.setCompanyName(dimCoalMineBasicInfoPo.getMqShortName());
            minePersonDataDto.setCompanyCode(dimCoalMineBasicInfoPo.getMqShortName());
            //井下人数
            minePersonDataDto.setCount(peopleNumber.toString());
            //井下领导数
            minePersonDataDto.setLeaderNumber(leadNumber.toString());
            //特种作业人数
            minePersonDataDto.setSpecial(specialNumber.toString());
            if (isOverStaffMine) {
                //超员矿井
                minePersonDataDto.setOverman("是");
            } else {
                minePersonDataDto.setOverman("否");
            }
            //带班领导
            minShiftList.forEach(tt -> {
                if (StringUtils.isBlank(minePersonDataDto.getShiftLead())) {
                    //是空的 第一次 追加
                    minePersonDataDto.setShiftLead(tt);
                } else {
                    //第二次 追加 前面携带逗号
                    minePersonDataDto.setShiftLead(minePersonDataDto.getShiftLead() + "," + tt);
                }
            });

            //井下人员
            coutList.add(minePersonDataDto);
            //带班领导数
            shiftLeadNumberList.add(minePersonDataDto);
            //特种作业人数
            specialNumberList.add(minePersonDataDto);
            //超员矿井数
            if (isOverStaffMine) {
                //超员矿井
                overStaffMineNumberList.add(minePersonDataDto);
            }
            //超200人矿井
            if (peopleNumber >= 200) {
                //超200人矿井
                overTwoHundredMineNumberList.add(minePersonDataDto);
            }
            //井下领导
            if (leadNumber > 0) {
                //井下领导数 只展示有井下领导的煤矿
                leaderNumberList.add(minePersonDataDto);
            }
            //灾害严重且超员煤矿数
            if (severeDisasterOverStaffMine && isOverStaffMine) {
                //灾害严重矿井
                severeDisasterOverStaffMineNumberList.add(minePersonDataDto);
            }
            if (minShiftNumber == 0) {
                //无领导带班
                noShiftLeadNumberNumberList.add(minePersonDataDto);
            }
        });
        //构建返回列表
        vo.setCoutList(coutList);
        vo.setSpecialNumberList(specialNumberList);
        vo.setLeaderNumberList(leaderNumberList);
        vo.setOverStaffMineNumberList(overStaffMineNumberList);
        vo.setOverTwoHundredMineNumberList(overTwoHundredMineNumberList);
        vo.setSevereDisasterOverStaffMineNumberList(severeDisasterOverStaffMineNumberList);
        vo.setShiftLeadNumberList(shiftLeadNumberList);
        vo.setNoShiftLeadNumberNumberList(noShiftLeadNumberNumberList);
        //解决无领导带班数量与list不一致问题
        vo.setNoShiftLeadNumberNumber(noShiftLeadNumberNumberList.size());
        return vo;

    }
    //判断当前煤矿是否是灾害严重矿井
    private boolean isSevereDisasterOverStaffMine(String disaster) {
        if (StringUtils.isEmpty(disaster)) {
            return false;
        }
        if (disaster.contains("冲击")) {
            return true;
        }
        if (disaster.contains("水文复杂") || disaster.contains("水文级复杂")) {
            return true;
        }
        if (disaster.contains("高瓦斯") || disaster.contains("低瓦斯") || disaster.contains("瓦斯突出")) {
            return true;
        }
        return false;

    }

    //获取龙软领导带班
    @Override
    public TreeSet<String> getMindutyList(String mineCode) {
        //根据煤矿编码获取当前煤矿名称
        JyglDwmcEntityPo jyglDwmcEntityPo = jyglDwmcEntityMapper.selectOne(new LambdaQueryWrapper<JyglDwmcEntityPo>().eq(JyglDwmcEntityPo::getOrgId, mineCode));
        if (jyglDwmcEntityPo == null) {
            return new TreeSet<>();
        }
        String mineName = jyglDwmcEntityPo.getFourdw();

        List<JyglInsertShiftManage1Po> jyglInsertShiftManage1Pos = jyglInsertShiftManage1Mapper.selectList(new LambdaQueryWrapper<JyglInsertShiftManage1Po>().eq(JyglInsertShiftManage1Po::getDw, mineName));
        if (jyglInsertShiftManage1Pos.isEmpty()) {
            return new TreeSet<>();
        }
        //获取时段信息
        JyglInsertShiftManage1Po jyglInsertShiftManage1Po = jyglInsertShiftManage1Pos.get(0);
        //获取当前煤矿带值班信息
        List<JyglShiftManagementPo> fineDataBeOnWatchListBymineName = jyglShiftManagementMapper.getFineDataBeOnWatchListBymineName(mineName);

        TreeSet<String> minShiftList = new TreeSet<>();

        if (fineDataBeOnWatchListBymineName != null) {
            //判断当前班次
            String classes = getShiftTimeList(new Date(), jyglInsertShiftManage1Po);
            //获取带班领导信息
            minShiftList = getMinShiftListByClasses(fineDataBeOnWatchListBymineName, classes);
            //获取带班领导数
        }
        return minShiftList;
    }

    /**
     * pc-人员定位-井下实时监测
     * @return
     */
    @Override
    public List<MinePersonDataDto> shaftMonitoring(String companyCode,String mineCode) {
        //获取所有井工矿列表
        List<SysOrganization> sysOrganizations = aqjkService.selectCodeUndergroundMiningSubMineList(companyCode,mineCode);
        if (sysOrganizations == null) {
            return new ArrayList<>();
        }
        List<MinePersonDataDto> dataDtos = sysOrganizations.stream().map(t -> {
            MinePersonDataDto minePersonDataDto = new MinePersonDataDto();
            //井下人数
            String number = dwdRydwRyssHMapper.selectPersonNumberList(t.getOrgCode()).toString();
            minePersonDataDto.setCount(number);
            //带班领导
            TreeSet<String> mindutyList = getMindutyList(t.getOrgCode());
            String shiftLead = leadListTurnString(mindutyList);
            minePersonDataDto.setShiftLead(shiftLead);
            minePersonDataDto.setMineCode(t.getOrgCode());
            minePersonDataDto.setMineName(t.getOrgName());
            return minePersonDataDto;
        }).collect(Collectors.toList());
        return dataDtos;
    }

    /**
     * pc-人员定位-超员矿井变化趋势
     * @param type 月|周
     * @return
     */
    @Override
    public PointDateListVo changeTrendOfOvermannedMine(String companyCode,String mineCode,String type) {
        //获取煤矿
        List<SysOrganization> sysOrganizations = aqjkService.selectCodeSubMineList(companyCode, mineCode);
        List<String> stringList = sysOrganizations.stream().map(t -> {
            return t.getOrgCode();
        }).collect(Collectors.toList());

        Map<String, String> mineMap = new HashMap<>();
        //转成map
        sysOrganizations.forEach(t -> {
            mineMap.put(t.getOrgCode(), t.getOrgName());
        });

        PointDateListVo vo = new PointDateListVo();
        List<String> dateList = new ArrayList<>();
        List<String> numberList = new ArrayList<>();
        List<List<DwsRyDdwRyDFDto>> list = new ArrayList<>();
        //获取今天的日期
        Date now = new Date();
        //获取开始日期
        Date startDate = null;
        if ("周".equals(type)) {
            //默认七天
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(now, -7);
        } else if ("月".equals(type)) {
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addMonths(now, -1);
        } else {
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(now, -7);
        }
        // 转换 Date 到 LocalDate
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate nowLocalDate = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //判断开始时间到现在有多少天
        // 计算天数
        long days = ChronoUnit.DAYS.between(startLocalDate, nowLocalDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < days; i++) {
            Date date = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(startDate, i);
            String format = dateFormat.format(date);
            dateList.add(format);
            //获取当天的所有数据
            List<DwsRyDdwRyDFPo> mineDwsRydwListByDate = dwsService.getMineDwsRydwListByDate(stringList,date);
            List<DwsRyDdwRyDFPo> collect = mineDwsRydwListByDate.stream().filter(t -> t.getIsCy() == 1).collect(Collectors.toList());
            numberList.add(String.valueOf(collect.size()));
            List<DwsRyDdwRyDFDto> dtos = collect.stream().map(t -> {
                DwsRyDdwRyDFDto dto = new DwsRyDdwRyDFDto();
                BeanUtil.copyProperties(t, dto);
                String mineName = mineMap.get(t.getMkbm());
                dto.setMineCode(t.getMkbm());
                dto.setMineName(mineName != null ? mineName : t.getMkbm());
                return dto;
            }).collect(Collectors.toList());
            list.add(dtos);
        }
        vo.setDateList(dateList);
        vo.setNumberList(numberList);
        vo.setList(list);
        return vo;
    }

    /**
     * pc-人员定位-井下超员变化趋势
     * @param type
     * @return
     */
    @Override
    public PointDateListVo changeTrendOfUndergroundPersonnel(String companyCode,String mineCode,String type) {
        List<SysOrganization> sysOrganizations = aqjkService.selectCodeUndergroundMiningSubMineList(companyCode, mineCode);
        List<String> stringList = sysOrganizations.stream().map(t -> {
            return t.getOrgCode();
        }).collect(Collectors.toList());

        PointDateListVo vo = new PointDateListVo();
        List<String> dateList = new ArrayList<>();
        //获取领导
        List<String> numberList = new ArrayList<>();
        //最大井下人
        List<String> maxnumberList = new ArrayList<>();
        //获取今天的日期
        Date now = new Date();
        //获取开始日期
        Date startDate = null;
        if ("周".equals(type)) {
            //默认七天
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(now, -7);
        } else if ("月".equals(type)) {
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addMonths(now, -1);
        } else {
            startDate = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(now, -7);
        }
        // 转换 Date 到 LocalDate
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate nowLocalDate = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        //判断开始时间到现在有多少天
        // 计算天数
        long days = ChronoUnit.DAYS.between(startLocalDate, nowLocalDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < days; i++) {
            Date date = net.hn.hnms.sys.common.core.utils.DateUtils.addDays(startDate, i);
            String format = dateFormat.format(date);
            dateList.add(format);
            //获取当天的所有数据
            List<DwsRyDdwRyDFPo> mineDwsRydwListByDate = dwsService.getMineDwsRydwListByDate(stringList,date);
            if (mineDwsRydwListByDate != null) {
                // 对字段求和
                int sumLd = mineDwsRydwListByDate.stream()
                        .mapToInt(po -> po.getMaxLdRyCnt() != null ? po.getMaxLdRyCnt() : 0) // 处理可能的null值
                        .sum();
                int sumRy = mineDwsRydwListByDate.stream()
                        .mapToInt(po -> po.getMaxRyCnt() != null ? po.getMaxRyCnt() : 0) // 处理可能的null值
                        .sum();

                numberList.add(String.valueOf(sumLd));
                maxnumberList.add(String.valueOf(sumRy));
            } else {
                // 处理空列表的情况
                numberList.add("0");
                maxnumberList.add("0");
            }

        }
        vo.setDateList(dateList);
        //最大领导
        vo.setNumberList(numberList);
        //最大井下人数
        vo.setMaxnumberList(maxnumberList);
        return vo;
    }

    /**
     * pc-人员定位-今日报警
     * @return
     */
    @Override
    public TodayAlarmVo todayAlarm(String companyCode, String mineCode) {
        List<SysOrganization> sysOrganizations = aqjkService.selectCodeSubMineList(companyCode, mineCode);
        List<String> stringList = sysOrganizations.stream().map(t -> {
            return t.getOrgCode();
        }).collect(Collectors.toList());
        TodayAlarmVo vo = new TodayAlarmVo();
        // 获取今天的开始和结束时间
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDayTime = today.atStartOfDay();
        LocalDateTime endOfDayTime = today.plusDays(1).atStartOfDay(); // 第二天的开始时间，即今天的结束时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startOfDay = startOfDayTime.format(formatter);
        String endOfDay = endOfDayTime.format(formatter);


        //人员求救次数
//        Long qjNumber = dwdRyDdwRyqjRtDFMapper.selectCount(new LambdaQueryWrapper<DwdRyDdwRyqjRtDFPo>()
//                .ge(DwdRyDdwRyqjRtDFPo::getSjscsjTimestamp, startOfDay)
//                .lt(DwdRyDdwRyqjRtDFPo::getSjscsjTimestamp, endOfDay));
        List<DwdRydwRyqjDistinctRtDPo> dwdRydwRyqjDistinctRtDPos = dwdRydwRyqjDistinctRtDMapper.selectList(new LambdaQueryWrapper<DwdRydwRyqjDistinctRtDPo>()
                .ge(DwdRydwRyqjDistinctRtDPo::getQjkssj, startOfDay)
                .le(DwdRydwRyqjDistinctRtDPo::getQjkssj, endOfDay)
                .in(!stringList.isEmpty(), DwdRydwRyqjDistinctRtDPo::getMkbm, stringList));
        vo.setPersonHelpCount(String.valueOf(dwdRydwRyqjDistinctRtDPos.size()));
        List<TodayAlarmDto> qjList = dwdRydwRyqjDistinctRtDPos.stream().map(t -> {
            TodayAlarmDto dto = new TodayAlarmDto();
            dto.setMineName(t.getKjmc());
            dto.setMineCode(t.getMkbm());
            //报警开始时间
            dto.setAlarmStart(t.getQjkssj());
            //报警结束时间
            dto.setAlarmEnd(t.getQjjssj());
            //人员卡编码
            dto.setRykbm(t.getRykbm());
            //姓名
            dto.setXm(t.getXm());
            //报警类型
            dto.setAlarmType("人员求救");
            //入井时间
            dto.setRjsk(t.getRjsj());
            //进入当前区域时间
            dto.setJrqysk(t.getJrdqqysk());
            //区域编码
            dto.setQybm(t.getDqszqybm());
            //区域名称
            DwdRydwRyqyDPo dwdRydwRyqyDPo = dwdRydwRyqyDMapper.selectOneByMineCodeQybm(t.getMkbm(), t.getDqszqybm());
            if (dwdRydwRyqyDPo != null) {
                dto.setQymc(dwdRydwRyqyDPo.getQymc()!= null?dwdRydwRyqyDPo.getQymc():"");
            } else {
                dto.setQymc(t.getDqszqybm());
            }
            //进入基站时间
            dto.setJrjzsk(t.getJrdqscjzsk());
            //基站编码
            dto.setJzbm(t.getDqszjzbm());
            //基站名称
            DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDMapper.selectOneByMineCodeJzbm(t.getMkbm(),t.getDqszjzbm());
            if (dwdRydwRyjzDPo != null) {
                dto.setJzmc(dwdRydwRyjzDPo.getJzmc()!= null?dwdRydwRyjzDPo.getJzmc():"");
            } else {
                dto.setJzmc(t.getDqszjzbm());
            }
            return dto;
        }).collect(Collectors.toList());
        vo.setPersonHelpCountList(qjList);

        //人员超时次数
//        Long csNumber = dwdRyDdwRycsRtDFMapper.selectCount(new LambdaQueryWrapper<DwdRyDdwRycsRtDFPo>()
//                .ge(DwdRyDdwRycsRtDFPo::getSjscsjTimestamp, startOfDay)
//                .lt(DwdRyDdwRycsRtDFPo::getSjscsjTimestamp, endOfDay));
        List<DwdRydwRycsDistinctRtDPo> dwdRydwRycsDistinctRtDPos = dwdRydwRycsDistinctRtDMapper.selectList(new LambdaQueryWrapper<DwdRydwRycsDistinctRtDPo>()
                .ge(DwdRydwRycsDistinctRtDPo::getBjkssj, startOfDay)
                .lt(DwdRydwRycsDistinctRtDPo::getBjkssj, endOfDay)
                .in(!stringList.isEmpty(), DwdRydwRycsDistinctRtDPo::getMkbm, stringList));

        vo.setPersonOverTimeCount(String.valueOf(dwdRydwRycsDistinctRtDPos.size()));
        List<TodayAlarmDto> csList = dwdRydwRycsDistinctRtDPos.stream().map(t -> {
            TodayAlarmDto dto = new TodayAlarmDto();
            dto.setMineName(t.getKjmc());
            dto.setMineCode(t.getMkbm());
            //报警开始时间
            dto.setAlarmStart(t.getBjkssj());
            //报警结束时间
            dto.setAlarmEnd(t.getBjjssj());
            //人员卡编码
            dto.setRykbm(t.getRykbm());
            //姓名
            dto.setXm(t.getXm());
            //报警类型
            dto.setAlarmType("人员超时");
            //入井时间
            dto.setRjsk(t.getRjsk());
            //进入当前区域时间
            dto.setJrqysk(t.getJrdqscqysj());
            //区域编码
            dto.setQybm(t.getQybm());
            //区域名称
            DwdRydwRyqyDPo dwdRydwRyqyDPo = dwdRydwRyqyDMapper.selectOneByMineCodeQybm(t.getMkbm(), t.getQybm());
            if (dwdRydwRyqyDPo != null) {
                dto.setQymc(dwdRydwRyqyDPo.getQymc()!= null?dwdRydwRyqyDPo.getQymc():"");
            } else {
                dto.setQymc(t.getQybm());
            }
            //进入基站时间
            dto.setJrjzsk(t.getJrdqscjzsk());
            //基站编码
            dto.setJzbm(t.getJzbm());
            //基站名称
            DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDMapper.selectOneByMineCodeJzbm(t.getMkbm(),t.getJzbm());
            if (dwdRydwRyjzDPo != null) {
                dto.setJzmc(dwdRydwRyjzDPo.getJzmc()!= null?dwdRydwRyjzDPo.getJzmc():"");
            } else {
                dto.setJzmc(t.getJzbm());
            }
            return dto;
        }).collect(Collectors.toList());
        vo.setPersonOverTimeCountList(csList);

        //区域超员次数
//        Long qycyNumber = dwdRyDdwRycyRtDFMapper.selectCount(new LambdaQueryWrapper<DwdRyDdwRycyRtDFPo>()
//                .ge(DwdRyDdwRycyRtDFPo::getSjscsjTimestamp, startOfDay)
//                .lt(DwdRyDdwRycyRtDFPo::getSjscsjTimestamp, endOfDay));
        List<DwdRydwRycyDistinctRtDPo> dwdRydwRycyDistinctRtDPos = dwdRydwRycyDistinctRtDMapper.selectList(new LambdaQueryWrapper<DwdRydwRycyDistinctRtDPo>()
                .ge(DwdRydwRycyDistinctRtDPo::getBjkssj, startOfDay)
                .lt(DwdRydwRycyDistinctRtDPo::getBjkssj, endOfDay)
                .in(!stringList.isEmpty(), DwdRydwRycyDistinctRtDPo::getMkbm, stringList));
        vo.setOvermannedCount(String.valueOf(dwdRydwRycyDistinctRtDPos.size()));
        vo.setOvermannedCountList(dwdRydwRycyDistinctRtDPos);

        //进入限制区域有人次数
//        Long xzqyNumber = dwdRyDdwRyxzRtDFMapper.selectCount(new LambdaQueryWrapper<DwdRyDdwRyxzRtDFPo>()
//                .ge(DwdRyDdwRyxzRtDFPo::getSjscsjTimestamp, startOfDay)
//                .lt(DwdRyDdwRyxzRtDFPo::getSjscsjTimestamp, endOfDay));
        List<DwdRydwRyxzDistinctRtDPo> dwdRydwRyxzDistinctRtDPos = dwdRydwRyxzDistinctRtDMapper.selectList(new LambdaQueryWrapper<DwdRydwRyxzDistinctRtDPo>()
                .ge(DwdRydwRyxzDistinctRtDPo::getBjkssj, startOfDay)
                .le(DwdRydwRyxzDistinctRtDPo::getBjkssj, endOfDay)
                .in(!stringList.isEmpty(), DwdRydwRyxzDistinctRtDPo::getMkbm, stringList));
        vo.setLimitAreaCount(String.valueOf(dwdRydwRyxzDistinctRtDPos.size()));
        List<TodayAlarmDto> xzList = dwdRydwRyxzDistinctRtDPos.stream().map(t -> {
            TodayAlarmDto dto = new TodayAlarmDto();
            dto.setMineName(t.getKjmc());
            dto.setMineCode(t.getMkbm());
            //报警开始时间
            dto.setAlarmStart(t.getBjkssj());
            //报警结束时间
            dto.setAlarmEnd(t.getBjjssj());
            //人员卡编码
            dto.setRykbm(t.getRykbm());
            //姓名
            dto.setXm(t.getXm());
            //报警类型
            dto.setAlarmType("进入限制区域");
            //入井时间
            dto.setRjsk(t.getRjsj());
            //进入当前区域时间
            dto.setJrqysk(t.getJrdqqysk());
            //区域编码
            dto.setQybm(t.getDqszqybm());
            //区域名称
            DwdRydwRyqyDPo dwdRydwRyqyDPo = dwdRydwRyqyDMapper.selectOneByMineCodeQybm(t.getMkbm(), t.getDqszqybm());
            if (dwdRydwRyqyDPo != null) {
                dto.setQymc(dwdRydwRyqyDPo.getQymc()!= null?dwdRydwRyqyDPo.getQymc():"");
            } else {
                dto.setQymc(t.getDqszqybm());
            }
            //进入基站时间
            dto.setJrjzsk(t.getJrdqscjzsk());
            //基站编码
            dto.setJzbm(t.getDqszjzbm());
            //基站名称
            DwdRydwRyjzDPo dwdRydwRyjzDPo = dwdRydwRyjzDMapper.selectOneByMineCodeJzbm(t.getMkbm(),t.getDqszjzbm());
            if (dwdRydwRyjzDPo != null) {
                dto.setJzmc(dwdRydwRyjzDPo.getJzmc()!= null?dwdRydwRyjzDPo.getJzmc():"");
            } else {
                dto.setJzmc(t.getDqszjzbm());
            }
            return dto;
        }).collect(Collectors.toList());
        vo.setLimitAreaCountList(xzList);
        return vo;
    }

    //领导列表转字符串
    public String leadListTurnString(TreeSet<String> mindutyList) {
        if (mindutyList.isEmpty()) {
            return "";
        }
        String dutyString = "";
        //带班领导
        for (String tt : mindutyList) {
            if (StringUtils.isBlank(dutyString)) {
                //是空的 第一次 追加
                dutyString = tt;
            } else {
                //第二次 追加 前面携带逗号
                dutyString=(dutyString + "," + tt);
            }
        }
        return dutyString;
    }

    //根据班次获取值班领导名称
    private TreeSet<String> getMindutyListByClasses(List<JyglShiftManagementPo> fineDataBeOnWatchListBymineName,String classes) {
        TreeSet<String> list = new TreeSet<>();
        fineDataBeOnWatchListBymineName.forEach(t->{
            String classes1 = t.getClasses1();
            if ("全天".equals(classes1)) {
                //全天值班
                list.add(t.getLeader1());
            } else {
                if (StringUtils.isNotEmpty(classes) && classes1.contains(classes)) {
                    list.add(t.getLeader1());
                }
            }
        });
        return list;
    }
    //根据班次获取带班领导名称
    @Override
    public TreeSet<String> getMinShiftListByClasses(List<JyglShiftManagementPo> fineDataBeOnWatchListBymineName,String classes) {
        TreeSet<String> list = new TreeSet<>();
        fineDataBeOnWatchListBymineName.forEach(t->{
            if (StringUtils.isNotEmpty(t.getClasses2())) {
                String classes2 = t.getClasses2();
                if ("全天".equals(classes2)) {
                    //全天值班
                    if (StringUtils.isNotEmpty(t.getLeader2())) {
                        list.add(t.getLeader2());
                    }
                } else {
                    if (StringUtils.isNotEmpty(classes) && classes2.contains(classes)) {
                        if (StringUtils.isNotEmpty(t.getLeader2())) {
                            list.add(t.getLeader2());

                        }
                    }
                }
            }

        });
        return list;
    }


    //根据时间获取班次时段集合
    @Override
    public String getShiftTimeList(Date dateTime,JyglInsertShiftManage1Po jyglInsertShiftManage1Po) {
        //早班开始时间
        List<String> morningTime = new ArrayList<>();
        //中班开始时间
        List<String> middleTime = new ArrayList<>();
        //晚班开始时间
        List<String> nightTime = new ArrayList<>();
        //后夜班开始时间
        List<String> afterNightTime = new ArrayList<>();

        if (jyglInsertShiftManage1Po != null) {

            if (StringUtils.isNotBlank(jyglInsertShiftManage1Po.getMornStart()) && StringUtils.isNotBlank(jyglInsertShiftManage1Po.getMornStop())) {
                morningTime.add(jyglInsertShiftManage1Po.getMornStart());
                morningTime.add(jyglInsertShiftManage1Po.getMornStop());
            }
            if (StringUtils.isNotBlank(jyglInsertShiftManage1Po.getMidStart()) && StringUtils.isNotBlank(jyglInsertShiftManage1Po.getMidStop())) {
                middleTime.add(jyglInsertShiftManage1Po.getMidStart());
                middleTime.add(jyglInsertShiftManage1Po.getMidStop());
            }
            if (StringUtils.isNotBlank(jyglInsertShiftManage1Po.getEvenStart()) && StringUtils.isNotBlank(jyglInsertShiftManage1Po.getEvenStop())) {
                nightTime.add(jyglInsertShiftManage1Po.getEvenStart());
                nightTime.add(jyglInsertShiftManage1Po.getEvenStop());
            }
            if (StringUtils.isNotBlank(jyglInsertShiftManage1Po.getEvenLstart()) && StringUtils.isNotBlank(jyglInsertShiftManage1Po.getEvenLstop())) {
                afterNightTime.add(jyglInsertShiftManage1Po.getEvenLstart());
                afterNightTime.add(jyglInsertShiftManage1Po.getEvenLstop());
            }
        }
        //时间段集合
        Map<String, List<String>> clases = new HashMap<>();
        clases.put("早", morningTime);
        clases.put("中", middleTime);
        clases.put("夜", nightTime);
        clases.put("后夜", afterNightTime);
        //判断现在时间是数据那个班
        String nowTime = DateUtils.format(dateTime, "HH:mm");
        String key = this.determineShift(nowTime, clases);
        return key;
    }

    // 判断当前时间属于哪个班次的方法
    private String determineShift(String nowTime, Map<String, List<String>> clases) {
        String shift = "";
        for (Map.Entry<String, List<String>> entry : clases.entrySet()) {
            String key = entry.getKey();
            List<String> value = entry.getValue();
            if (value.isEmpty() || value.size() != 2) {
                //不用判断  没有时间段  直接跳过比较
                continue;
            }
            boolean between = this.isBetween(nowTime, value);
            if (between) {
                shift = key;
            }
        }
        return shift;
    }

    // 辅助方法，判断时间是否在指定范围内
    private boolean isBetween(String nowTime,List<String> times) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String startTime = times.get(0);
            String endTime = times.get(1);
            Date now = sdf.parse(nowTime);
            Date start = sdf.parse(startTime);
            Date end = sdf.parse(endTime);

            // 如果结束时间跨越了午夜边界  0：00
            if (end.before(start)) {
                Calendar endCal = Calendar.getInstance();
                endCal.setTime(end);
                endCal.add(Calendar.DAY_OF_MONTH, 1); // 添加一天
                end = endCal.getTime();
            }

            // 判断当前时间是否在开始时间和结束时间之间
            return now.compareTo(start) >= 0 && now.compareTo(end) < 0;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据公司编码获取所有子煤矿编码
     *
     * @param dto
     * @return
     */
    private List<String> getMineCodeListByCompanyCode(DwdBaseDto dto) {
        List<String> mineCodeList = new ArrayList<>();

        if (StringUtils.isNotEmpty(dto.getCompanyCode()) && StringUtils.isEmpty(dto.getMineCode())) {
            //获取当前公司的所有煤矿
            List<SysOrganization> mineCodeListByCompanyCode = aqjkService.getMineCodeListByCompanyCode(dto.getCompanyCode());
            if (mineCodeListByCompanyCode != null) {
                mineCodeList = mineCodeListByCompanyCode.stream().map(obj -> obj.getOrgCode()).collect(Collectors.toList());
            }
        } else if (StringUtils.isNotEmpty(dto.getMineCode())){
            mineCodeList.add(dto.getMineCode());
        }
        return mineCodeList;
    }

    /**
     * 处理 pageQuery 空值问题
     *
     * @param pageQuery
     * @return
     */
    private PageQuery dealWithPageQuery(PageQuery pageQuery) {
        if (pageQuery.getPageNum() == null) {
            pageQuery.setPageNum(0);
        }
        if (pageQuery.getPageSize() == null) {
            pageQuery.setPageSize(10);
        }

        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? pageQuery.getPageNum() * pageQuery.getPageSize() : 0);
        return pageQuery;
    }

    /**
     * 设置煤矿名称，公司编码，公司名称信息
     *
     * @param list
     * @return
     */
    private List<? extends DwdBaseVo> setMineAndCompanyInfo(List<? extends DwdBaseVo> list) {
        //获取所有煤矿列表
        List<DimCoalMineBasicInfoPo> mineList = aqjkService.selectCodeSubMineInfoList();
        Map<String, DimCoalMineBasicInfoPo> collect = mineList.stream()
                .collect(Collectors.toMap(DimCoalMineBasicInfoPo::getMkCode, org -> org));

        list.forEach(t -> {
            String mkbm = t.getMineCode();
            DimCoalMineBasicInfoPo dimCoalMineBasicInfoPo = collect.get(mkbm);
            if (dimCoalMineBasicInfoPo != null) {
                t.setMineName(dimCoalMineBasicInfoPo.getMkFullName());
                t.setCompanyCode(dimCoalMineBasicInfoPo.getMqCode());
                t.setCompanyName(dimCoalMineBasicInfoPo.getMqFullName());
            }
        });
        return list;
    }


}
